

prj_base := $(shell pwd)

subdirs := 

libdir := $(prj_base)/lib
objdir := $(prj_base)/obj
target := user.proxy

#cross := powerpc-wrs-linux-
#CC = $(cross)gcc  
#CC = $(CC)

dyn_libs := -lpthread -lrt -lm -ldl 
#-lstdc++
sta_libs :=   
ldflags := $(LDFLAGS) -Wl,--export-dynamic 
defs := -DBIOS_APP=1
inc := -I.
g_cflags  := $(CFLAGS) -g -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -rdynamic -funroll-loops -Wno-packed-bitfield-compat -fPIC \
           -fvisibility=hidden \
           --sysroot=$(sysroot) \
           $(defs) \
           $(inc)
cflags := $(g_cflags)

sources := $(wildcard *.c)  
objects := $(patsubst %.c, $(objdir)/%.o, $(sources))  
dependence := $(objects:.o=.d)  

all: $(mkdirs) $(objects)
	@for subdir in $(subdirs); do make -C $$subdir $@ prj_base=$(prj_base) g_cflags="$(g_cflags)" cross=$(cross) || exit "$$?"; done	
	$(CC) -o $(prj_base)/$(target) $(objdir)/*.o -Wl,-Bstatic $(sta_libs) -Wl,-Bdynamic $(dyn_libs) $(ldflags) $(libs)

mkdirs:
	@if [ ! -e $(objdir) ]; then mkdir $(objdir); fi
# 	@if [ ! -e $(libdir) ]; then mkdir $(libdir); fi

$(objdir)/%.o: %.c  
	$(CC) $(cflags) -c $< -o $@  

-include $(dependence)

$(objdir)/%.d: %.c
	set -e; rm -f $@; \
        $(CC) -MT $(objdir)/$*.o -MM $(cflags) $< > $@.$$$$; \
        sed 's,\($*\).o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
        rm -f $@.$$$$

.PHONY: clean

clean:  
	rm -f $(objdir)/*
